package qldc.pm.ksdl.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBException;
import javax.xml.bind.PropertyException;

import qldc.pm.csdl.cd90.model.CongDan;
import qldc.pm.csdl.cd90.service.CongDanLocalServiceUtil;
import qldc.pm.ksdl.util.KSDLConstants;
import qldc.pm.ksdl.util.KSDLUtil;

import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.servlet.ServletResponseUtil;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.Constants;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;

public class DanhSachDanCuAction {
	
	private static final Log LOG = LogFactoryUtil.getLog(DanhSachDanCuAction.class);
	
	public static void submitDSDCForm(ActionRequest actionRequest, ActionResponse actionResponse) throws SystemException{
		ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
		boolean signedIn = themeDisplay.isSignedIn();

		if(signedIn){
			String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
			
			String tuNgayString = ParamUtil.getString(actionRequest, "tuNgay", null);
			String denNgayString = ParamUtil.getString(actionRequest, "denNgay", null);
			
			String soCMND = ParamUtil.getString(actionRequest, "soCMND");
			String soDinhDanh = ParamUtil.getString(actionRequest, "soDinhDanh");
			String diaChiThuongTru = ParamUtil.getString(actionRequest, "diaChiThuongTru");
			
			long tinhId = ParamUtil.getLong(actionRequest, "tinhId");
			long quanHuyenId = ParamUtil.getLong(actionRequest, "quanHuyenId");
			long phuongXaId = ParamUtil.getLong(actionRequest, "phuongXaId");
			
			//int start = ParamUtil.getInteger(actionRequest, "start", 0);
			//int end = ParamUtil.getInteger(actionRequest, "end", 0);
			
			boolean isAbleToSearch = true;
			
			Date tuNgay = null;
			Date denNgay = null;
			
			try {
				if(Validator.isNotNull(tuNgayString)) {
					tuNgay = KSDLUtil.parseDate(tuNgayString);
				}
				
				if(Validator.isNotNull(denNgayString)) {
					denNgay = KSDLUtil.parseDate(denNgayString);
				}
				
				// Add success message if there is no error occurs
				//SessionMessages.add(actionRequest.getPortletSession(), "yeu-cau-cua-ban-duoc-thuc-hien-thanh-cong");
			} catch (ParseException e) {
				SessionErrors.add(actionRequest, "gap-loi-xu-ly-vui-long-dien-dung-format-ngay-dd-mm-yyyy");
				isAbleToSearch = false;
			}
			
			if(cmd.equals(KSDLConstants.IN)){
//				List<HoSoKDCoDK> hoSoList = new ArrayList<HoSoKDCoDK>();
//				
//				if(isAbleToSearch){
//					hoSoList = KSDLUtil.getHoSo(maSoHoSo, null, trangThaiHoSoId, loaiHoSoId, hoVaTen, nhanTuNgay, nhanDenNgay, traTuNgay, traDenNgay, nhomNganhNgheId, nganhNgheId, trangThaiXuLy, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
//				}
//				
//				try {
//					printHoSoList(actionRequest, actionResponse, hoSoList);
//					
//					SessionMessages.add(actionRequest.getPortletSession(), "yeu-cau-cua-ban-duoc-thuc-hien-thanh-cong");
//				} catch (Exception e) {
//					LOG.error("Error while printing", e);
//					SessionErrors.add(actionRequest, "gap-loi-xu-ly-vui-long-xem-server-log");
//				}
//				
//				actionResponse.setRenderParameter("mainTabs", KSDLConstants.TIM_HO_SO);
			} else if (cmd.equals(KSDLConstants.EXPORT)){
				try {
					List<CongDan> congDanList = CongDanLocalServiceUtil.findByDDCN_DCTT_NTT_CMND(tinhId, quanHuyenId, phuongXaId, diaChiThuongTru, soDinhDanh, soCMND, tuNgay, denNgay, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
					
					// Temp dir of Tomcat
					String tmpDir = System.getProperty("java.io.tmpdir");
					
					File xmlFile = new File(tmpDir + File.separator + "DanhSachCongDan_" + System.currentTimeMillis() + ".xml");
					
					KSDLUtil.exportCongDan(congDanList, xmlFile);
					
					// Send file to user for download
					HttpServletRequest httpServletRequest = PortalUtil.getHttpServletRequest(actionRequest);
					HttpServletResponse httpServletResponse = PortalUtil.getHttpServletResponse(actionResponse);
					ServletResponseUtil.sendFile(httpServletRequest, httpServletResponse, xmlFile.getName(), new FileInputStream(xmlFile), "application/xml");
					
				} catch (SystemException e) {
					LOG.error(e);
				} catch (PropertyException e) {
					LOG.error(e);
				} catch (JAXBException e) {
					LOG.error(e);
				} catch (FileNotFoundException e) {
					LOG.error(e);
				} catch (IOException e) {
					LOG.error(e);
				}
				
			} else {
				actionResponse.setRenderParameter("tinhId", String.valueOf(tinhId));
				actionResponse.setRenderParameter("quanHuyenId", String.valueOf(quanHuyenId));
				actionResponse.setRenderParameter("phuongXaId", String.valueOf(phuongXaId));
				
				actionResponse.setRenderParameter("diaChiThuongTru", diaChiThuongTru);
				actionResponse.setRenderParameter("soCMND", soCMND);
				actionResponse.setRenderParameter("soDinhDanh", soDinhDanh);
				
				actionResponse.setRenderParameter("tuNgay", tuNgayString);
				actionResponse.setRenderParameter("denNgay", denNgayString);
				actionResponse.setRenderParameter("isAbleToSearch", String.valueOf(isAbleToSearch));
				
				actionResponse.setRenderParameter("mainTabs", KSDLConstants.QUAN_LY_DAN_CU_90);
			}
		}
		
		// Hide default error message
		SessionMessages.add(actionRequest, PortalUtil.getPortletId(actionRequest) + SessionMessages. KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);
	}
}
